package com.base.myproject.client.CRM;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com 操作联系人资料
 * 
 */
public class ContactMaterial extends BaseJDBCPanel {
	BaseStore<BusiObject> store;

	Button fuzzy_search = new Button("查询");// 查询按钮
	String sql = "select * from oa_contact";

	BaseComboBoxForm<BaseModelData> sectionCode = new BaseComboBoxForm<BaseModelData>(
			"id",
			"name",
			"SystemInfo",
			"where name is not null and id is not null and len(id)=(select max(len(id)) from systeminfo)",
			true);

	public ContactMaterial() {
		init();
	}

	public void init() {
		initWeigetx();
		registerMainStore(store);
		fuzzy_search.addListener(Events.OnClick, getListener());
	}

	private void initWeigetx() {
		BorderLayoutData southData = new BorderLayoutData(LayoutRegion.SOUTH,
				150);
		southData.setCollapsible(true);
		southData.setFloatable(true);
		southData.setSplit(true);
		southData.setMargins(new Margins(0, 5, 0, 5));

		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 5, 5));

		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(5);

		vp.add(createForm());
		HorizontalPanel s = new HorizontalPanel();
		s.add(fuzzy_search);
		vp.add(s);

		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.createGrid());
		vp.add(panel2);

		ContentPanel content = new ContentPanel();
		content.setScrollMode(Scroll.AUTO);
		content.setHeaderVisible(false);
		content.add(vp);

		ContentPanel south = new ContentPanel();
		south.setHeading("新增资料填写");
		south.add(createResultForm());

		add(content, centerData);
		add(south, southData);
	}

	BaseFormPanel createResultForm() {
		BaseFormPanel form = new BaseFormPanel(getStore());
		TableLayout tl = new TableLayout(8);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("业务员:"),td);
		TextField<String> C_num2  = new TextField<String>(); 
		C_num2.setName("salesman");
		form.add(C_num2,td);
		form.add(new Label("类别:"),td);
		BaseComboBoxForm<ModelData> C_num3 = new BaseComboBoxForm<ModelData>("code","name","dictionary"," where type = 'cm_type'",true);
		C_num3.setName("type");
		form.add(C_num3,td);
		form.add(new Label("姓名:"),td);
		TextField<String> C_num4  = new TextField<String>(); 
		C_num4.setName("name");
		form.add(C_num4,td);
		form.add(new Label("email1:"),td);
		TextField<String> C_num5  = new TextField<String>(); 
		C_num5.setName("email1");
		form.add(C_num5,td);
		form.add(new Label("email2:"),td);
		TextField<String> C_num6  = new TextField<String>(); 
		C_num6.setName("email2");
		form.add(C_num6,td);
		form.add(new Label("email3:"),td);
		TextField<String> C_num7  = new TextField<String>(); 
		C_num7.setName("email3");
		form.add(C_num7,td);
		form.add(new Label("手机1:"),td);
		 NumberField C_num8  = new NumberField(); 
		C_num8.setPropertyEditorType(Integer.class); 
		C_num8.setName("phone1");
		form.add(C_num8,td);
		form.add(new Label("手机2:"),td);
		 NumberField C_num9  = new NumberField(); 
		C_num9.setPropertyEditorType(Integer.class); 
		C_num9.setName("phone2");
		form.add(C_num9,td);
		form.add(new Label("手机3:"),td);
		 NumberField C_num10  = new NumberField(); 
		C_num10.setPropertyEditorType(Integer.class); 
		C_num10.setName("phone3");
		form.add(C_num10,td);
		form.add(new Label("公司电话:"),td);
		TextField<String> C_num11  = new TextField<String>(); 
		C_num11.setName("companyPhone");
		form.add(C_num11,td);
		form.add(new Label("传真:"),td);
		TextField<String> C_num12  = new TextField<String>(); 
		C_num12.setName("fax");
		form.add(C_num12,td);
		form.add(new Label("家里电话:"),td);
		TextField<String> C_num13  = new TextField<String>(); 
		C_num13.setName("homePhone");
		form.add(C_num13,td);
		form.add(new Label("公司地址:"),td);
		TextField<String> C_num14  = new TextField<String>(); 
		C_num14.setName("address");
		form.add(C_num14,td);
		form.add(new Label("公司名:"),td);
		TextField<String> C_num15  = new TextField<String>(); 
		C_num15.setName("companyName");
		form.add(C_num15,td);
		form.add(new Label("职位:"),td);
		TextField<String> C_num16  = new TextField<String>(); 
		C_num16.setName("position");
		form.add(C_num16,td);
		form.add(new Label("部门:"),td);
		TextField<String> C_num17  = new TextField<String>(); 
		C_num17.setName("department");
		form.add(C_num17,td);
		form.setBodyBorder(false);
		 form.inited(); 
		return form;
	};

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("salesman");
		type.getField("salesman").setType(String.class);
		type.addField("type");
		type.getField("type").setType(String.class);
		type.addField("name");
		type.getField("name").setType(String.class);
		type.addField("email1");
		type.getField("email1").setType(String.class);
		type.addField("email2");
		type.getField("email2").setType(String.class);
		type.addField("email3");
		type.getField("email3").setType(String.class);
		type.addField("phone1");
		type.getField("phone1").setType(Integer.class);
		type.addField("phone2");
		type.getField("phone2").setType(Integer.class);
		type.addField("phone3");
		type.getField("phone3").setType(Integer.class);
		type.addField("companyPhone");
		type.getField("companyPhone").setType(String.class);
		type.addField("fax");
		type.getField("fax").setType(String.class);
		type.addField("homePhone");
		type.getField("homePhone").setType(String.class);
		type.addField("address");
		type.getField("address").setType(String.class);
		type.addField("companyName");
		type.getField("companyName").setType(String.class);
		type.addField("position");
		type.getField("position").setType(String.class);
		type.addField("department");
		type.getField("department").setType(String.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("oa_contact");
		info.setKeyname(new String[] { "icode" });
		info.setSql(sql);
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGrid<BusiObject> grid = null;
	CheckBoxSelectionModel<BusiObject> cbs;

	BaseGrid<BusiObject> createGrid() {
		cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C2 = new ColumnConfig("salesman", "业务员", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("type", "类别", 80);
		GridCellRenderer<ModelData> renderer1 = new GridCellRenderer<ModelData>() {
			public String render(ModelData model, String property,
					ColumnData d, int rowIndex, int colIndex,
					ListStore<ModelData> store, Grid<ModelData> grid) {
				if (model.get(property) != null
						&& model.get(property) instanceof ModelData) {
					return ((ModelData) model.get(property)).get("name"); // 返回model里面的name属性
				}
				return model.get(property);
			}
		};
		C3.setRenderer(renderer1);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("name", "客户姓名", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("email1", "email1", 80);
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("email2", "email2", 80);
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("email3", "email3", 80);
		columns.add(C7);
		ColumnConfig C8 = new ColumnConfig("phone1", "手机1", 80);
		columns.add(C8);
		ColumnConfig C9 = new ColumnConfig("phone2", "手机2", 80);
		columns.add(C9);
		ColumnConfig C10 = new ColumnConfig("phone3", "手机3", 80);
		columns.add(C10);
		ColumnConfig C11 = new ColumnConfig("companyPhone", "公司电话", 80);
		columns.add(C11);
		ColumnConfig C12 = new ColumnConfig("fax", "传真", 80);
		columns.add(C12);
		ColumnConfig C13 = new ColumnConfig("homePhone", "家里电话", 80);
		columns.add(C13);
		ColumnConfig C14 = new ColumnConfig("address", "公司地址", 80);
		columns.add(C14);
		ColumnConfig C15 = new ColumnConfig("companyName", "公司名称", 80);
		columns.add(C15);
		ColumnConfig C16 = new ColumnConfig("position", "职位", 80);
		columns.add(C16);
		ColumnConfig C17 = new ColumnConfig("department", "部门", 80);
		columns.add(C17);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm);
		Tools.setResize(grid, 0.97, 0.73);
		cbs.setSelectionMode(SelectionMode.MULTI);
		grid.setSelectionModel(cbs);
		grid.addPlugin(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
		return grid;
	}

	FormPanel form;
	TextField<String> salesman = new TextField<String>(); // 业务员名称
	BaseComboBoxForm<BaseModelData> type = new BaseComboBoxForm<BaseModelData>("code","name","dictionary"," where type = 'cm_type'",true); // 类别
	TextField<String> name = new TextField<String>(); // 客户名字
	TextField<String> phone = new TextField<String>(); // 手机号
	TextField<String> companyPhone = new TextField<String>(); // 公司电话
	TextField<String> homePhone = new TextField<String>(); // 家里电话
	TextField<String> companyName = new TextField<String>(); // 公司名
	TextField<String> position = new TextField<String>(); // 职位名
	

	private FormPanel createForm() {
		form = new FormPanel();

		TableLayout tl = new TableLayout(8);

		form.setWidth(913);
		form.setPadding(4);
		form.setBodyBorder(false);
		form.setHeaderVisible(false);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		form.add(new Label("业务员:"), td);
		form.add(salesman, td);

		form.add(new Label("类别:"), td);
		form.add(type, td);

		form.add(new Label("姓名:"), td);
		form.add(name, td);

		form.add(new Label("手机号:"), td);
		form.add(phone, td);

		form.add(new Label("公司电话:"), td);
		form.add(companyPhone, td);

		form.add(new Label("家里电话:"), td);
		form.add(homePhone, td);

		form.add(new Label("公司名:"), td);
		form.add(companyName, td);

		form.add(new Label("职位:"), td);
		form.add(position, td);

		form.setLayout(tl);

		return form;
	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.OnClick) {
							String where = "";
							String nameStr = name.getRawValue();
							String positionStr = position.getRawValue();
							String salesmanStr = salesman.getRawValue();
							String phoneStr = phone.getRawValue();
							String companyPhoneStr = companyPhone.getRawValue();
							String homePhoneStr = homePhone.getRawValue();
							String companyNameStr = companyName.getRawValue();
							String typeStr = "";
							
							if (type.getValue() != null) {
								typeStr = type.getValue().get("code");
							}
							// 模糊查询和精确查询共用的where语句条件
							if (!salesmanStr.equals("")) {
								where = where + " and salesman like '%25" + salesmanStr
										+ "%25'";
							}
							
							if (!nameStr.equals("")) {
								where = where + " and name like '%25" + nameStr
										+ "%25'";
							}
							
							if (!positionStr.equals("")) {
								where = where + " and position like '%25" + positionStr
										+ "%25'";
							}
							
							if (!phoneStr.equals("")) {
								where = where + " and (phone1 like '%25" + phoneStr
										+ "%25' or phone2 like '%25" + phoneStr
										+ "%25' or phone3 like '%25" + phoneStr + "%25')";
							}
							
							if (!companyPhoneStr.equals("")) {
								where = where + " and companyPhone like '%25" + companyPhoneStr
										+ "%25'";
							}
							
							if (!homePhoneStr.equals("")) {
								where = where + " and homePhone like '%25" + homePhoneStr
										+ "%25'";
							}
							
							if (!companyNameStr.equals("")) {
								where = where + " and companyName like '%25" + companyNameStr
										+ "%25'";
							}
							
							if (!typeStr.equals("")) {
								where = where + " and type = '" + typeStr
										+ "'";
							}
							
								refreshGrid(where);
							}
						}
				} finally {
					sync--;
				}

			}

		};
		return defaultlistener;
	}

	/**
	 * @param where
	 *            查询得到的where条件 定义可多次使用的sql，通过传进来的where语句进行查询
	 */
	private void refreshGrid(String where) {
		// 可重用的SQL，用于查询出所有还没安排面试的申请表
		String sql = "select * from oa_contact where 1=1" + where;
		grid.getStore().getStoreinfo().setSql(sql);
		grid.getStore().reload();
	}
}
